/**
 * 音乐相关控制逻辑
 */
const path = require('path')

const express = require('express')
const formidable = require('formidable')

const router = module.exports = express.Router()
router.prefix = '/admin/album'

const fs = require('../../libs/fs')
const Album = require('../../models/album')
const Artist = require('../../models/artist')

/**
 * GET /album/
 */
router.get('/', async(req, res) => {

    var albums = await Album.findAll({
        include: { model: Artist, as: 'artist' }
    });

    res.locals.title = '专辑列表'
    res.locals.list = albums
    res.render('admin/album/list')


})

/**
 * GET /album/list
 */
router.get('/list', async(req, res) => {

    var albums = await Album.findAll({
        include: { model: Artist, as: 'artist' }
    });

    res.locals.title = '专辑列表'
    res.locals.list = albums
    res.render('admin/album/list')


})

/**
 * GET /album/add
 */
router.get('/add', async(req, res) => {

    var artists = await Artist.findAll();
    res.locals.title = '添加新专辑'
    res.locals.artists = artists
    res.render('admin/album/add')

})

/**
 * POST /album/add
 */
router.post('/add', (req, res) => {
    const form = new formidable.IncomingForm()
    let upload_dir = path.join(global.UPLOAD_PATH, 'album');
    fs.mkdirsSync(upload_dir)
    form.uploadDir = upload_dir
    form.keepExtensions = true
    form.parse(req, (error, fields, files) => {
        if (error) throw error

        let poster_url = fs.getUrl(path.basename(files.poster.path), 'album')

        // 接收到客户端提交过来文件和填写的信息
        Album.create({
            name: fields.name,
            poster: poster_url,
            publishDate: fields.publishDate,
            playCount: fields.playCount,
            artistId: parseInt(fields.artistId),
        })

        res.redirect('/admin/album')

    })


})

/**
 * GET /album/edit/:id
 */
router.get('/edit/:id', async(req, res) => {
    res.locals.title = '编辑'
        // 接收传过来的ID
    const id = parseInt(req.params.id || 0)
    if (!id) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }
    // 找到数组中的这个元素删除
    let album = await Album.findByPk(id)
    if (!album) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }

    var artists = await Artist.findAll();
    res.locals.artists = artists
    res.locals.item = album
    res.render('admin/album/edit')


})

/**
 * POST /album/edit/:id
 */
router.post('/edit/:id', async(req, res) => {

    // 接收传过来的ID
    const id = parseInt(req.params.id || 0)
    if (!id) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }
    // 找到数组中的这个元素删除
    const album = await Album.findByPk(id)
    if (!album) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }


    const form = new formidable.IncomingForm()
    let upload_dir = path.join(global.UPLOAD_PATH, 'album');
    fs.mkdirsSync(upload_dir)
    form.uploadDir = upload_dir
    form.keepExtensions = true
    form.parse(req, (error, fields, files) => {
        if (error) throw error

        let poster_url = fs.getUrl(path.basename(files.poster.path), 'album')

        album.update({
            name: fields.name,
            poster: poster_url,
            publishDate: fields.publishDate,
            artistId: fields.artistId,
            playCount: fields.playCount,
        })

        res.redirect('/admin/album')

    })

})

/**
 * GET /album/delete/:id
 */
router.get('/delete/:id', (req, res) => {
    // 接收传过来的ID
    const id = parseInt(req.params.id || 0)
    if (!id) {
        // 不存在这个数据
        return res.status(404).send('没有该记录')
    }
    // 找到数组中的这个元素删除
    Album.destroy({
        where: {
            id: id
        }
    }).then(() => {
        res.redirect('/admin/album')
    })



})